--炸金花启动引导
--设置窗口
module("UI.Windows.SAIBAOGameWin", package.seeall)

local UIUtility = require("UI.Utility")

Debug.log("骰宝开始引导初始化")
local UIWindow = require("UI.UIWindow").UIWindow
local GameObject = UnityEngine.GameObject
local DOTweenMgr = require("UI.DOTweenMgr")
local ChipTool = require("ChipTool").ChipTool
local SBModule = require("Module.SBModule").SBModule
local atools = require("StringTool").StringTool
local ClubModule = require("Module.ClubModule").ClubModule
local WWWTexture = require("NetWork.NetHttp").WWWTexture
local TableTool = require("TableTool").TableTool


SAIBAOGameWin = UIWindow:new()
SAIBAOGameWin.name = "SAIBAOGameWin"
SAIBAOGameWin.tableId = 1
SAIBAOGameWin.groupKey = 1
SAIBAOGameWin.isInit = false

function SAIBAOGameWin:OnShow()

end

function SAIBAOGameWin:Init()
    local t = self.Container.transform
    self.exitBtn = UIUtility.GetChildTransform("exitBtn", t, true)

    self.sendBtn1 = UIUtility.GetChildTransform("sendBtn1", t, true)

    self.chipTargetSpArr = {}
    self.chipTargetPosArr = {}
    self.chipTargetLableArr = {}
    self.chipTragetBgArr = {}
    self.chipTargetEffArr = {}
    for i = 1, 31 do
        self.chipTargetSpArr[i] = UIUtility.FindContorl("UISprite", "chipTarget" .. i, t)
        self.chipTargetLableArr[i] = UIUtility.FindContorl("UILabel", "selfBetNum", self.chipTargetSpArr[i].transform)
        self.chipTargetPosArr[i] = UIUtility.GetChildTransform("chipTarget" .. i, t, true)
        self.chipTragetBgArr[i] = UIUtility.GetChildTransform("selfBet_NumBg", self.chipTargetSpArr[i].transform, true)
        self.chipTargetEffArr[i] = UIUtility.GetChildTransform("selectEff", self.chipTargetSpArr[i].transform, true)
    end

    self.sendChipBtnArr = {}
    for i = 1, 5 do
        self.sendChipBtnArr[i] = UIUtility.GetChildTransform("sendChipBtn" .. i, t, true)
    end

    self.tempChipObj      = UIUtility.GetChildTransform("tempChipObj", t, true)
    self.tempChipObjSP    = UIUtility.FindContorl("UISprite", "tempChipObj", t)
    self.tempChipPanel    = UIUtility.GetChildTransform("tempChipPanel", t, true)

    self.tempWinChipObj   = UIUtility.GetChildTransform("tempWinChipObj", t, true)
    self.tempWinChipObjSP = UIUtility.FindContorl("UISprite", "tempWinChipObj", t)
    self.tempWinChipPanel = UIUtility.GetChildTransform("tempWinChipPanel", t, true)

    self.playerPos        = UIUtility.GetChildTransform("playerPos", t, true)
    self.selfHeadicon     = UIUtility.FindContorl("UITexture", "selfHeadicon", t)
    self.selfPlayerName   = UIUtility.FindContorl("UILabel", "selfPlayerName", t)


    self.tempheadPanel = UIUtility.GetChildTransform("tempheadPanel ", t, true)

    self.curChipIcon   = UIUtility.GetChildTransform("curChipIcon", t, true)
    self.curChipIconSP = UIUtility.FindContorl("UISprite", "curChipIcon", t)



    self.tempHeadObj    = UIUtility.GetChildTransform("tempHeadObj", t, true)
    self.betAni         = UIUtility.GetChildTransform("betAni", t, true)

    --选择筹码Panel
    self.SendChipPanel  = UIUtility.GetChildTransform("SendChipPanel", t, true)
    --

    --遍历数组
    --飞行筹码
    self.flyChipSp      = UIUtility.FindContorl("UISprite", "flyChip", t)

    --下注确认
    self.verifyBtn      = UIUtility.GetChildTransform("verifyBtn", t, true)
    self.verifyBtnSP    = UIUtility.FindContorl("UISprite", "verifyBtn", t)

    --取消下注
    self.cancelBtn      = UIUtility.GetChildTransform("cancelBtn", t, true)
    self.cancelBtnSP    = UIUtility.FindContorl("UISprite", "cancelBtn", t)
    --重复下注
    self.repeatBtn      = UIUtility.GetChildTransform("repeatBtn", t, true)
    --重复SP
    self.repeatBtnSP    = UIUtility.FindContorl("UISprite", "repeatBtn", t)

    --自己下注数量
    self.selfBetChipNum = UIUtility.FindContorl("UILabel", "selfBetChipNum", t)
    --自己筹码总量
    self.selfChipNum    = UIUtility.FindContorl("UILabel", "selfChipNum", t)


    self.StartSBBtn     = UIUtility.GetChildTransform("StartGame", t, true)

    --下注阶段名字
    self.statusStepName = UIUtility.FindContorl("UILabel", "statusStepName", t)
    --时间倒计时
    self.clock_num      = UIUtility.FindContorl("UISprite", "clock_num", t)
    self.clock_num1     = UIUtility.FindContorl("UISprite", "clock_num1", t)
    self.clock_num2     = UIUtility.FindContorl("UISprite", "clock_num2", t)

    --所有筹码数量
    self.ChipNumText    = UIUtility.FindContorl("UILabel", "ChipNumText", t)

    self.diceArrSP      = {}
    --骰子数组
    for i = 1, 3 do
        self.diceArrSP[i] = UIUtility.FindContorl("UISprite", "dice" .. i, t)
    end

    --荷官位置
    self.dealerPos = UIUtility.GetChildTransform("dealerPos", t, true)
    --荷官Sp位置
    self.dealerSPPos = UIUtility.FindContorl("UISprite", "dealerSPPos", t)

    --自己赢金币位置
    self.selfWinGoldPos = UIUtility.GetChildTransform("selfWinGoldPos", t, true)
    --获取Spine动画
    self.spinePeopleAni = UIUtility.FindContorl("SpineCtrl", "spinePeopleAni", t)
    self.spineHandAni = UIUtility.FindContorl("SpineCtrl", "spineHandAni", t)

    --获取加金币动画
    self.selfChipNumAniPos = UIUtility.FindContorl("TweenPosition", "selfChipNumAni", t)
    self.selfChipNumAniAlpha = UIUtility.FindContorl("TweenAlpha", "selfChipNumAni", t)
    self.selfChipNumAniText = UIUtility.FindContorl("UILabel", "selfChipNumAni", t)

    self.selfChipNumAniPos.gameObject:SetActive(true)
    self.selfChipNumAniPos:ResetToBeginning()
    self.selfChipNumAniPos:PlayForward()
    self.selfChipNumAniAlpha.gameObject:SetActive(true)
    self.selfChipNumAniAlpha:ResetToBeginning()
    self.selfChipNumAniAlpha:PlayForward()

    --路子
    self.luziGrid = UIUtility.GetChildTransform("luziGrid", t, true)
    --路子Item
    self.luziItem = UIUtility.GetChildTransform("luziItem", t, true)



    -- UIHelper.BindUIEvnet("Click", function()
    --     --生成赢的筹码发给玩家
    --     local chipArr = ChipTool:analyzeChip(300)
    --     for chipArrIndex = 1, #chipArr do
    --         local curChip = self:GetWinChip(chipArr[chipArrIndex]);
    --         curChip.gameObject:SetActive(true)

    --         ChipTool:ChipMoveStartPosRandomAni(curChip, self.selfWinGoldPos.transform.position,
    --             self.dealerSPPos, true, function()
    --                 curChip.gameObject:SetActive(false)
    --             end)
    --     end
    -- end, UIUtility.GetChildTransform("testBtn", t, true).gameObject)

    UIHelper.BindUIEvnet("Click", function()
        --生成赢的筹码发给玩家
        local firstObj = self.luziGrid.transform:GetChild(0).gameObject
        firstObj:SetActive(true)
        local x = self.luziGrid.transform:GetChild(19).gameObject.transform.localPosition.x

        firstObj.transform.parent = self.luziGrid.transform.parent
        firstObj.transform.parent = self.luziGrid.transform
        firstObj.transform.localPosition = Vector3(x + 50, 0, 0)

        DOTweenMgr.DTDOMove(
            self.luziGrid.transform,
            Vector3(self.luziGrid.transform.localPosition.x - 50, -14, 0),
            0.5,
            1,
            false,
            nil,
            nil,
            0, 5)
    end, UIUtility.GetChildTransform("testBtn", t, true).gameObject)

    self:InitData()
    self:BindEvents()
    self.isInit = true
    if self.tableId ~= 1 then
        ClubModule.JoinRoomCm(self.tableId, self.groupKey)
    end
end

function SAIBAOGameWin:InitData()
    self.hideChipArr = {}
    self.showChipArr = {}

    --记录自己下注的临时筹码对象
    self.selfTempChipNameArr = {}

    --记录自己下注的筹码对象
    self.selfChipNameArr = {}
    --记录上局下注筹码对象
    self.selfUpChipNameArr = {}

    --selfChip_1_200 表示下注区域在1
    --所以筹码放一起， 通过名字区分
    --记录自己下注


    self.headFristPosX = 300;
    self.isHeadLeft = math.random(1, 2) > 1.5                                           --初始位置
    self.headPosX = self.isHeadLeft and math.random(417, 500) or math.random(960, 1063) ---250  250
    self.headFristPosX = self.isHeadLeft and 300 or 1193

    self.headPosY = math.random(-400, 600); --750  780

    self.chipNum = { 25, 50, 100, 500, 1000 }
    self.upSelectTarget = nil

    self.selfUserId = 0

    self.statusStepName1 = { "等待下注", "开始下注", "结算中", "结算中2" }

    self.betAreKeyValue = { "1_0", "2_0", "3_0", "4_0", "5_0",
        "6_1", "6_2", "6_3", "6_4", "6_5", "6_6",
        "7_1", "7_2", "7_3", "7_4", "7_5", "7_6",
        "8_1", "8_2", "8_3", "8_4", "8_5", "8_6", "8_7",
        "8_8", "8_9", "8_10", "8_11", "8_12", "8_13", "8_14" }

    for i = 1, #self.chipTargetEffArr do
        self.chipTargetEffArr[i].gameObject:SetActive(false)
        self.chipTargetLableArr[i].text = 0
        self.chipTragetBgArr[i].gameObject:SetActive(false)
    end

    print("SB数据初始化完成")
end

function SAIBAOGameWin:getBetAreIndex(name)
    for i = 1, #self.betAreKeyValue do
        if self.betAreKeyValue[i] == name then
            return i
        end
    end
end

function SAIBAOGameWin:remove_empty_values(tbl)
    local new_tbl = {}
    local index = 1
    for k, v in pairs(tbl) do
        if v ~= nil then
            new_tbl[index] = v
            index = index + 1
        end
    end
    return new_tbl
end

-- chipColor 筹码颜色 ChipValue 筹码值
function SAIBAOGameWin:GetChip(_chipColor, _chipValue, _parent)
    if #self.hideChipArr == 0 then
        local tempChip = GameObject.Instantiate(self.tempChipObj)
        tempChip.transform.parent = _parent
        tempChip.transform.localScale = Vector3.one
        tempChip.transform.localPosition = Vector3.zero
        tempChip.transform.localEulerAngles = Vector3.zero
        tempChip.transform:GetComponent("UISprite").spriteName = _chipValue


        local chipLable = UIUtility.FindContorl("UILabel", "chipValue", tempChip.transform)
        chipLable.text = _chipColor
        self.showChipArr[#self.showChipArr + 1] = tempChip
    else
        local tempChip = self.hideChipArr[#self.hideChipArr]
        tempChip.gameObject:SetActive(true)
        tempChip.transform.parent = _parent
        tempChip.transform.localScale = Vector3.one
        tempChip.transform.localPosition = Vector3.zero
        tempChip.transform.localEulerAngles = Vector3.zero
        tempChip.transform:GetComponent("UISprite").spriteName = _chipValue

        local chipLable = UIUtility.FindContorl("UILabel", "chipValue", tempChip.transform)
        chipLable.text = _chipColor
        self.showChipArr[#self.showChipArr + 1] = self.hideChipArr[#self.hideChipArr]
        self.hideChipArr[#self.hideChipArr] = nil
    end
    self.showChipArr[#self.showChipArr].gameObject.name = "otherChip"
    return self.showChipArr[#self.showChipArr]
end

--获取头像
function SAIBAOGameWin:GetHead(headUrl, name)
    local tempChip = nil
    for i = 0, self.tempheadPanel.transform.childCount - 1 do
        if self.tempheadPanel.transform:GetChild(i).gameObject.activeSelf == false then
            tempChip = self.tempheadPanel.transform:GetChild(i)
            tempChip.gameObject:SetActive(true)
            break
        end
    end
    if tempChip == nil then
        tempChip = GameObject.Instantiate(self.tempHeadObj)
    end
    tempChip.transform.parent = self.tempheadPanel
    tempChip.transform.localScale = Vector3.one
    tempChip.transform.localPosition = Vector3.zero
    tempChip.transform.localEulerAngles = Vector3.zero

    UIUtility.FindContorl("UILabel", "name", tempChip.transform).text = name
    local headIcon = UIUtility.FindContorl("UITexture", "headIcon", tempChip.transform)
    headIcon.gameObject:SetActive(false)
    function callBackPlayeAni(isOk, www)
        if isOk and headIcon ~= nil then
            headIcon.mainTexture = www.texture
            headIcon.gameObject:SetActive(true)
        end
    end

    WWWTexture(headUrl, callBackPlayeAni)
    return tempChip
end

--获取赢的筹码
function SAIBAOGameWin:GetWinChip(value)
    local tempChip = nil
    for i = 0, self.tempWinChipPanel.transform.childCount - 1 do
        if self.tempWinChipPanel.transform:GetChild(i).gameObject.activeSelf == false then
            tempChip = self.tempWinChipPanel.transform:GetChild(i)
            tempChip.gameObject:SetActive(true)
            break
        end
    end
    if tempChip == nil then
        tempChip = GameObject.Instantiate(self.tempWinChipObj)
    end
    tempChip.transform.parent = self.tempWinChipPanel
    tempChip.transform.localScale = Vector3.one
    tempChip.transform.localPosition = Vector3.zero
    tempChip.transform.localEulerAngles = Vector3.zero

    tempChip.transform:GetComponent("UISprite").spriteName = value

    local chipLable = UIUtility.FindContorl("UILabel", "chipValue", tempChip.transform)
    chipLable.text = value
    return tempChip
end

function SAIBAOGameWin:RecoveryShowChip()
    local sumTime = 0.5
    local frequency = 0.1
    local frequencyNum = sumTime / frequency
    local oneNum = math.ceil(#self.showChipArr / frequencyNum)

    --假设 1内收起所有筹码
    for i = 1, #self.showChipArr do
        local inidex = #self.showChipArr - i + 1;
        if self.showChipArr[inidex] == nil then
            break
        end
        function aniEnd(_index)
            self.showChipArr[_index].transform.parent = self.tempChipPanel
            self.showChipArr[_index].gameObject:SetActive(false)
            self.hideChipArr[#self.hideChipArr + 1] = self.showChipArr[_index]
            if _index == #self.showChipArr then
                self.showChipArr = {}
            end
        end

        function delay()
            if self.showChipArr[inidex] then
                --传本地坐标
                ChipTool:ChipMoveAni(self.showChipArr[inidex], nil, self.dealerSPPos, true, aniEnd,
                    self.dealerPos.transform
                    .position, i)
            end
        end

        local t = frequency * (math.ceil(i / oneNum) - 1)
        CountDownMgr.Instance:CreateCountDown(t, t, delay)
    end
end

function SAIBAOGameWin:BindEvents()
    --退出游戏
    function OnClose()
        local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
        UIWinMgr:CloseWindow("SAIBAOGameWin")

        local NewGameLoobyWin = UIWinMgr:OpenWindow("NewGameLoobyWin")
        NewGameLoobyWin:ToClubPage()

        SBModule.QuitReq()
        self:Close()
    end

    UIHelper.BindUIEvnet("Click", OnClose, self.exitBtn.gameObject)

    --自己下注
    for i = 1, 31 do
        function OnSendChip()
            if self.tableStatus ~= 2 then
                return
            end

            local curChip = self:GetChip(self.curChipIconSP.spriteName, self.curChipIconSP.spriteName,
                self.tempChipPanel.transform);
            curChip.gameObject:SetActive(true)
            curChip.gameObject.name = "selfTempChip"
            self.selfTempChipNameArr[#self.selfTempChipNameArr + 1] = self.curChipIconSP.spriteName ..
                "_" .. self.betAreKeyValue[i]
            self:UpdateBtnStatus()
            --飞筹码
            ChipTool:ChipMoveAni(curChip, self.playerPos.position, self.chipTargetSpArr[i], true)
            --显示文本
            self.chipTragetBgArr[i].gameObject:SetActive(true)
            self.chipTargetLableArr[i].text = tonumber(self.chipTargetLableArr[i].text) +
                tonumber(self.curChipIconSP.spriteName)
            --目标区域高亮

            if self.upSelectTarget ~= nil then
                self.upSelectTarget:SetActive(false)
            end
            --选中光圈
            self.upSelectTarget = self.chipTargetEffArr[i].gameObject
            self.chipTargetEffArr[i].gameObject:SetActive(true)
        end

        UIHelper.BindUIEvnet("Click", OnSendChip, self.chipTargetPosArr[i].gameObject)
    end




    for i = 1, 5 do
        function starCall()

        end

        function endCall()
            self.flyChipSp.gameObject:SetActive(false)
            self.curChipIconSP.spriteName = self.flyChipSp.spriteName
            self.tweener = DOTweenMgr.DTDORot360(
                self.curChipIcon.transform,
                Vector3(0, 0, 720),
                1,
                1,
                function() end,
                function() end,
                0
            )
        end

        function selectChipHit()
            self.SendChipPanel.gameObject:SetActive(not self.SendChipPanel.gameObject.activeSelf)
            self.flyChipSp.transform:DOKill()
            -- self.flyChipSp.transform.localPosition=self.sendChipBtnArr[i].transform.localPosition;
            self.flyChipSp.transform.localPosition = self.flyChipSp.transform.parent.transform:InverseTransformPoint(
                self.sendChipBtnArr[i].transform.position)
            self.flyChipSp.gameObject:SetActive(true)
            self.flyChipSp.spriteName = tostring(self.chipNum[i])
            local tempPos = self.flyChipSp.transform.parent.transform:InverseTransformPoint(self.curChipIcon.transform
                .position)
            DOTweenMgr.DTDOMove(
                self.flyChipSp.transform,
                tempPos,
                0.3,
                1,
                false,
                starCall,
                endCall,
                0, 5)
        end

        UIHelper.BindUIEvnet("Click", selectChipHit, self.sendChipBtnArr[i].gameObject)

        --当前筹码点击
        function curChipHit()
            self.SendChipPanel.gameObject:SetActive(not self.SendChipPanel.gameObject.activeSelf)
        end

        UIHelper.BindUIEvnet("Click", curChipHit, self.curChipIcon.gameObject)




        --取消下注
        function cancelBtnHit()
            if self.cancelBtnSP.spriteName == "deleteOff" then
                return
            end
            for i = 1, #self.chipTargetEffArr do
                self.chipTargetEffArr[i].gameObject:SetActive(false)
                self.chipTargetLableArr[i].text = 0
                self.chipTragetBgArr[i].gameObject:SetActive(false)
            end
            local tempArr = {}
            for i = 1, #self.selfChipNameArr do
                local chipInfo = atools:Split(self.selfChipNameArr[i], "_")
                local index = self:getBetAreIndex(chipInfo[2] .. "_" .. chipInfo[3])
                if tempArr[index] ~= nil then
                    tempArr[index] = tempArr[index] + tonumber(chipInfo[1])
                else
                    tempArr[index] = tonumber(chipInfo[1])
                end
            end
            --显示金额
            for k, v in pairs(tempArr) do
                if v ~= nil then
                    self.chipTargetLableArr[k].text = v
                    self.chipTragetBgArr[k].gameObject:SetActive(true)
                end
            end

            --清空桌上自己下的筹码
            for i = 1, #self.showChipArr do
                if self.showChipArr[i].gameObject.name == "selfTempChip" then
                    self.showChipArr[i].gameObject:SetActive(false)
                    self.hideChipArr[#self.hideChipArr] = self.showChipArr[i]
                    self.showChipArr[i] = nil
                end
            end
            self.showChipArr = self:remove_empty_values(self.showChipArr)


            self.selfTempChipNameArr = {} --清空自己临时下注记录
            self:UpdateBtnStatus()
        end

        UIHelper.BindUIEvnet("Click", cancelBtnHit, self.cancelBtn.gameObject)
        --重复下注
        function repeatBtnHit()
            if self.repeatBtnSP.spriteName == "repeatOff" then
                return
            end
            if self.tableStatus ~= 2 then
                return
            end




            local tempString = ""
            for i = 1, #self.selfUpChipNameArr do
                tempString = tempString .. self.selfUpChipNameArr[i]
                if i ~= #self.selfUpChipNameArr then
                    tempString = tempString .. "#"
                end

                local chipInfo = atools:Split(self.selfUpChipNameArr[i], "_")

                local curChip = self:GetChip(chipInfo[1], chipInfo[1],
                    self.tempChipPanel.transform);
                curChip.gameObject:SetActive(true)
                curChip.gameObject.name = "selfTempChip"
                self.selfTempChipNameArr[#self.selfTempChipNameArr + 1] = self.selfUpChipNameArr[i]

                local areIndex = self:getBetAreIndex(chipInfo[2] .. "_" .. chipInfo[3])
                --飞筹码
                ChipTool:ChipMoveAni(curChip, self.playerPos.position, self.chipTargetSpArr[areIndex], true)
                --显示文本
                self.chipTragetBgArr[areIndex].gameObject:SetActive(true)
                self.chipTargetLableArr[areIndex].text = tonumber(self.chipTargetLableArr[areIndex].text) +
                    tonumber(chipInfo[1])
            end
            self:UpdateBtnStatus()
            -- SBModule.BetReq(101, tempString)
        end

        UIHelper.BindUIEvnet("Click", repeatBtnHit, self.repeatBtn.gameObject)


        --确认下注
        function verifyBtnHit()
            if self.verifyBtnSP.spriteName == "okOff" then
                return
            end
            --请求下注  游戏类型
            local tempString = ""
            for i = 1, #self.selfTempChipNameArr do
                tempString = tempString .. self.selfTempChipNameArr[i]
                if i ~= #self.selfTempChipNameArr then
                    tempString = tempString .. "#"
                end
            end
            SBModule.BetReq(101, tempString)
        end

        UIHelper.BindUIEvnet("Click", verifyBtnHit, self.verifyBtn.gameObject)



        UIHelper.BindUIEvnet("Click", function()
            SBModule.StartGameReqCm()
        end, self.StartSBBtn.gameObject)
    end
end

function SAIBAOGameWin:headAni(obj, targetPos, time, endCall, delay, easeType)
    function callback_start()
    end

    function callback_end()
        if endCall ~= nil then
            endCall()
        end
    end

    self.closeTweener = DOTweenMgr.DTDOMove(
        obj,
        targetPos,
        time,
        1,
        false,
        callback_start,
        callback_end,
        delay,
        easeType
    )
end

function SAIBAOGameWin:TableInfoRes(_data)
    print("玩家金币" .. _data.player.gold)
    self.selfUserId = _data.player.userId
    --更新路子
    local luziArr = _data.luzi
    if #luziArr >= 20 then
        for i = 0, self.luziGrid.transform.childCount - 1 do
            local tempItem = self.luziGrid.transform:GetChild(19 - i).gameObject
            local diceInfo = TableTool:split_string(luziArr[i + 1])
            for diceIndex = 1, 3 do
                local dice = UIUtility.FindContorl("UISprite", "" .. diceIndex, tempItem.transform)
                dice.spriteName = diceInfo[diceIndex]
            end
            tempItem.gameObject:SetActive(true)
            break
        end
    else
        for i = 0, 19 do
            local tempItem = self.luziGrid.transform:GetChild(19 - i).gameObject
            if i + 1 <= #luziArr then
                tempItem:SetActive(true)
                local diceInfo = TableTool:split_string(luziArr[i + 1])
                for diceIndex = 1, 3 do
                    local dice = UIUtility.FindContorl("UISprite", "" .. diceIndex, tempItem.transform)
                    dice.spriteName = diceInfo[diceIndex]
                end
            else
                tempItem:SetActive(false)
            end
        end
    end



    --更新阶段显示
    self:TableStateCast(_data.tableStatus, _data.countdown, false)
    --更新头像
    self.selfHeadicon.gameObject:SetActive(false)
    function callBackPlayeAni(isOk, www)
        if isOk and self.selfHeadicon ~= nil then
            self.selfHeadicon.mainTexture = www.texture
            self.selfHeadicon.gameObject:SetActive(true)
        end
    end
    --更新玩家名字
    self.selfPlayerName.text=_data.player.name

    WWWTexture(_data.player.icon, callBackPlayeAni)
    local selfBetNum = 0
    --更新筹码额
    if _data.player.betInfo ~= "" then
        local betInfo = atools:Split(_data.player.betInfo, "#")
        for i = 1, #betInfo do
            if betInfo[i] == "" then
                break
            end
            local areInfo = atools:Split(betInfo[i], "_")
            if areInfo[3] == nil then
                areInfo[3] = 0
            end
            local index = self:getBetAreIndex(areInfo[2] .. "_" .. areInfo[3])
            self.chipTragetBgArr[index].gameObject:SetActive(true)
            self.chipTargetLableArr[index].text = areInfo[1]
            betInfo = betInfo + areInfo[1]
        end
    end
    --更新自己金币总额
    self.selfChipNum.text = _data.player.gold
    --更新自己下注金额
    self.selfBetChipNum.text = selfBetNum

    --显示桌面所有筹码
    if _data.chipsInfo ~= "" and _data.tableStatus == 2 then
        for index = 1, #_data.chipsInfo do
            print("后端筹码" .. _data.chipsInfo[index])
            local chipsInfo = atools:Split(_data.chipsInfo[index], "#")
            for i = 1, #chipsInfo do
                local tempChipInfo = atools:Split(chipsInfo[i], "_")
                if tempChipInfo[3] == nil then
                    tempChipInfo[3] = 0
                end
                local index = self:getBetAreIndex(tempChipInfo[2] .. "_" .. tempChipInfo[3])
                local chipArr = ChipTool:analyzeChip(tonumber(tempChipInfo[1]))
                for chipArrIndex = 1, #chipArr do
                    local curChip = self:GetChip(chipArr[chipArrIndex], chipArr[chipArrIndex],
                        self.tempChipPanel.transform);
                    curChip.gameObject:SetActive(true)

                    local tempV3 = Vector3(
                        math.random(-self.chipTargetSpArr[index].width / 2 + 30,
                            self.chipTargetSpArr[index].width / 2 - 30),
                        math.random(-self.chipTargetSpArr[index].height / 2 + 30,
                            self.chipTargetSpArr[index].height / 2 - 30), 0)
                    tempV3 = curChip.transform.parent.transform:InverseTransformPoint(self.chipTargetSpArr[index]
                        .transform.position) + tempV3
                    curChip.transform.localPosition = tempV3;
                end
            end
        end
    end
end

--更新倒计时
function SAIBAOGameWin:TableStateCast(tableStatus, countdown, isShowAni)
    self.tableStatus = tableStatus
    self.ChipNumText.text = "筹码池数量:" .. self.tempChipPanel.transform.childCount
    if tableStatus == 0 then
        return
    end
    --动画步骤
    --idle2 摇骰子
    --start 开始下注
    -- idle1 待机状态

    --先end 结束下注
    --idle3 推出骰子   跟 2 播放idle3 要同时播放
    --等1秒   转播idle5

    if tableStatus == 2 then
        self:UpdateBtnStatus()
        if isShowAni == true then
            self.spinePeopleAni:DoAni("idle2", false, false, function()
                print("spine播放完成idle2")
                self.spinePeopleAni:DoAni("start", false, false, function()
                    print("spine播放完成start")
                    self.spinePeopleAni:DoAni("idle1", true, false)
                end)
            end)
        end
    end

    --回收筹码
    if tableStatus == 3 then

    end

    --更新阶段
    self.statusStepName.text = self.statusStepName1[tableStatus]

    local curtime = countdown

    function updateTime()
        self.clock_num.gameObject:SetActive(curtime < 10)
        self.clock_num1.gameObject:SetActive(curtime >= 10)
        self.clock_num2.gameObject:SetActive(curtime >= 10)
        --更新倒计时
        if curtime < 10 then
            self.clock_num.spriteName = "num_" .. curtime
        else
            self.clock_num1.spriteName = "num_" .. math.floor(curtime / 10)
            self.clock_num2.spriteName = "num_" .. math.floor(curtime % 10)
        end
    end

    updateTime()

    if self.countCallId ~= nil then
        CountDownMgr.Instance:RemoveCountDown(self.countCallId, false)
    end

    function delay()
        curtime = curtime - 1
        updateTime()
    end

    function countEnd()
        self.countCallId = nil
    end

    self.countCallId = CountDownMgr.Instance:CreateCountDown(curtime, 1, delay, countEnd)
end

--玩家下注广播
function SAIBAOGameWin:BetCast(_data)
    if _data.userId == self.selfUserId then
        print("--------------自己下注不用处理-------------")
        --自己下注  改为已经下注
        for i = 1, #self.selfTempChipNameArr do
            self.selfChipNameArr[#self.selfChipNameArr + 1] = self.selfTempChipNameArr[i]
        end
        for i = 1, #self.chipTargetEffArr do
            self.chipTargetEffArr[i].gameObject:SetActive(false)
        end
        --把所有临时下注改为下注
        for i = 1, #self.showChipArr do
            if self.showChipArr[i].gameObject.name == "selfTempChip" then
                self.showChipArr[i].gameObject.name = "selfChip"
            end
        end
        --更新下注总金额
        self.selfChipNum.text = _data.gold
        self.selfTempChipNameArr = {}
        self:UpdateBtnStatus()
    else
        -- if _data.userId ~= self.selfUserId then
        -- return
        -- end
        self.isHeadLeft = math.random(1, 2) > 1.5                                           --初始位置
        self.headPosX = self.isHeadLeft and math.random(417, 500) or math.random(960, 1063) ---250  250
        self.headFristPosX = self.isHeadLeft and 300 or 1193
        self.headPosY = math.random(-400, 600)
        local tempHead = self:GetHead(_data.icon, _data.name)
        for i = 1, #_data.betInfo do
            --print("后端返回区域" .. _data.betInfo[i].bet .. "_" .. _data.betInfo[i].betType .. "_" .. _data.betInfo[i].betOn)
            function playAni(isKill, _headPosX, _headPosY, _headFristPosX)
                if isKill == nil or isKill == true then
                    tempHead:DOKill()
                end

                self:headAni(tempHead, Vector3(_headPosX - 30, _headPosY, 0), 0.1, nil, 0, 1)
                self:headAni(tempHead, Vector3(_headPosX, _headPosY, 0), 0.1, nil, 0.1, 1)
                self:headAni(tempHead, Vector3(_headFristPosX, _headPosY, 0), 0.2, function()
                    tempHead.gameObject.name = "tempHeadObj"
                    tempHead.gameObject:SetActive(false)
                    --750  780
                end, 1, 1)
            end

            tempHead.name = "headObj1"

            tempHead.transform.localPosition = Vector3(self.headFristPosX, self.headPosY, 0)
            self:headAni(tempHead, Vector3(self.headPosX, self.headPosY, 0), 0.2,
                playAni(nil, self.headPosX, self.headPosY, self.headFristPosX), 0, 1)

            CountDownMgr.Instance:CreateCountDown(0.3, 0.3, function()
                local indexName = _data.betInfo[i].betType .. "_" .. _data.betInfo[i].betOn

                local chipArr = ChipTool:analyzeChip(_data.betInfo[i].bet)
                for chipArrIndex = 1, #chipArr do
                    local curChip = self:GetChip(chipArr[chipArrIndex], chipArr[chipArrIndex],
                        self.tempChipPanel.transform);
                    curChip.gameObject:SetActive(true)

                    ChipTool:ChipMoveAni(curChip, tempHead.transform.position,
                        self.chipTargetSpArr[self:getBetAreIndex(indexName)], true)
                end
            end)
            -- end
        end
    end
end

--更新 删除 重复下注 确认按钮状态   跟自己下注数量的显示
function SAIBAOGameWin:UpdateBtnStatus()
    --更新按钮状态
    self.cancelBtnSP.spriteName = # self.selfTempChipNameArr > 0 and "deleteOn" or "deleteOff"

    self.repeatBtnSP.spriteName = self.tableStatus == 2 and #self.selfUpChipNameArr > 0 and "repeatOn" or "repeatOff"

    self.verifyBtnSP.spriteName = #self.selfTempChipNameArr > 0 and "okOn" or "okOff"
    --自己下注总额  临时金额 +  已经下注金额
    local betChipNum = 0
    for index = 1, # self.selfTempChipNameArr do
        local betChipInfo = atools:Split(self.selfTempChipNameArr[index], "_")
        betChipNum = betChipNum + betChipInfo[1]
    end
    for index = 1, # self.selfChipNameArr do
        local betChipInfo = atools:Split(self.selfChipNameArr[index], "_")
        betChipNum = betChipNum + betChipInfo[1]
    end
    self.selfBetChipNum.text = betChipNum
end

--结果推送
function SAIBAOGameWin:ResultCast(_data)
    --idle2 摇骰子
    --start 开始下注
    -- idle1 待机状态

    --先end 结束下注
    --idle3 推出骰子   跟 2 播放idle3 要同时播放
    --等1秒   转播idle5
    --  if _data ~=2 then
    --     return
    --  end



    self.spinePeopleAni:DoAni("end", false, false, function()
        self.spinePeopleAni:DoAni("idle3", false, false, function()

        end)
        self.spineHandAni.transform.gameObject:SetActive(true)
        self.spineHandAni:DoAni("idle3", false, false, function()
            self.spineHandAni.transform.gameObject:SetActive(false)

            CountDownMgr.Instance:CreateCountDown(0.5, 0.5, function()
                self.spinePeopleAni:DoAni("idle5", false, false, function()
                    resultShow()
                    self.spinePeopleAni:DoAni("idle1", true, false, function()
                    end)
                end)
            end)
        end)
    end)


    --结果展示
    function resultShow()
        --显示结果
        local diceInfo = atools:Split(_data.result, "#")

        local resultInfo = atools:Split(diceInfo[1], "_")

        for i = 1, 3 do
            self.diceArrSP[i].spriteName = resultInfo[i]
        end
        if diceInfo[2] == "0" then
            self.chipTargetEffArr[5].gameObject:SetActive(true)
        end

        if diceInfo[2] ~= "0" then
            self.chipTargetEffArr[tonumber(diceInfo[2])].gameObject:SetActive(true)
        end
        if diceInfo[3] ~= "0" then
            self.chipTargetEffArr[tonumber(diceInfo[3]) + 2].gameObject:SetActive(true)
        end
        --三军  6开始
        if diceInfo[4] ~= "0" then
            local oneAreInfo = atools:Split(diceInfo[4], "_")
            for i = 1, #oneAreInfo do
                self.chipTargetEffArr[tonumber(oneAreInfo[i]) + 5].gameObject:SetActive(true)
            end
        end
        --围骰  12开始
        if diceInfo[5] ~= "0" then
            self.chipTargetEffArr[tonumber(diceInfo[5]) + 11].gameObject:SetActive(true)
        end
        --点数  18开始
        if diceInfo[6] ~= "0" then
            self.chipTargetEffArr[tonumber(diceInfo[6]) + 17].gameObject:SetActive(true)
        end



        function delay()
            self:RecoveryShowChip()
            for i = 1, #self.chipTargetEffArr do
                self.chipTargetEffArr[i].gameObject:SetActive(false)
                self.chipTargetLableArr[i].text = 0
                self.chipTragetBgArr[i].gameObject:SetActive(false)
            end
            self.selfBetChipNum.text = 0
            self.selfUpChipNameArr = self.selfChipNameArr
            self.selfChipNameArr = {}     --清空自己下注记录
            self.selfTempChipNameArr = {} --临时筹码
            self:UpdateBtnStatus()
            _data.winLoss = 300
            if _data.status ~= 10 then
                CountDownMgr.Instance:CreateCountDown(0.9, 0.9, function()
                    if _data.status == 0 then
                        --没下注不处理
                        -- self.selfChipNum.text = _data.balance
                    else
                        --生成赢的筹码发给玩家
                        local chipArr = ChipTool:analyzeChip(_data.winLoss)
                        for chipArrIndex = 1, #chipArr do
                            local curChip = self:GetWinChip(chipArr[chipArrIndex]);
                            curChip.gameObject:SetActive(true)

                            ChipTool:ChipMoveStartPosRandomAni(curChip, self.selfWinGoldPos.transform.position,
                                self.dealerSPPos, true, function()
                                    self.selfChipNumAniText.text = "+300"
                                    self.selfChipNumAniPos.gameObject:SetActive(true)
                                    self.selfChipNumAniPos:ResetToBeginning()
                                    self.selfChipNumAniPos:PlayForward()
                                    self.selfChipNumAniAlpha.gameObject:SetActive(true)
                                    self.selfChipNumAniAlpha:ResetToBeginning()
                                    self.selfChipNumAniAlpha:PlayForward()
                                    self.selfChipNum.text = _data.balance
                                    curChip.gameObject:SetActive(false)
                                end)
                        end
                    end
                    local firstObj = self.luziGrid.transform:GetChild(0).gameObject
                    firstObj:SetActive(true)
                    local x = self.luziGrid.transform:GetChild(19).gameObject.transform.localPosition.x

                    firstObj.transform.parent = self.luziGrid.transform.parent
                    firstObj.transform.parent = self.luziGrid.transform
                    firstObj.transform.localPosition = Vector3(x + 50, 0, 0)

                    DOTweenMgr.DTDOMove(
                        self.luziGrid.transform,
                        Vector3(self.luziGrid.transform.localPosition.x - 50, -14, 0),
                        0.5,
                        1,
                        false,
                        nil,
                        nil,
                        0, 5)
                end)
            end
            --总回收时间0.5
        end

        CountDownMgr.Instance:CreateCountDown(0.5, 0.5, delay)
    end
end
